package com.aiyige.page.detail.dialog;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.model.moment.entity.Guarantee;

import java.util.List;

/**
 * Created by xiachao on 2017/11/9.
 */

public class DialogSafeguard extends DialogFragment {
    View mView;
    List<Guarantee> data;
    int marginTop;

    public List<Guarantee> getData() {
        return data;
    }

    public void setData(List<Guarantee> data) {
        this.data = data;
    }

    public int getMarginTop() {
        return marginTop;
    }

    public void setMarginTop(int marginTop) {
        this.marginTop = marginTop;
    }

    public DialogSafeguard() {
        setStyle(STYLE_NORMAL, R.style.BottomDialogTheme);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_safeguard, null);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    public static DialogSafeguard newInstance(List<Guarantee> guaranteeList, int marginTop) {
        DialogSafeguard dialogSafeguard = new DialogSafeguard();
        dialogSafeguard.setData(guaranteeList);
        dialogSafeguard.setMarginTop(marginTop);
        return dialogSafeguard;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, Resources.getSystem().getDisplayMetrics().heightPixels - marginTop);
        }
    }

    private void init() {
        mView.findViewById(R.id.rl_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (data != null && !data.isEmpty()) {
            // 填充数据数据
            LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.ll_guarantee);
            int size = data.size();
            View itemView;
            TextView title;
            TextView detail;
            for (int i = 0; i < size; i++) {
                itemView = LayoutInflater.from(mView.getContext()).inflate(R.layout.item_guarantee, null);

                LinearLayout llSafeguardTitle = itemView.findViewById(R.id.ll_safeguard_title);
                if (i == 0) {
                    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) llSafeguardTitle.getLayoutParams();
                    ll.topMargin = 0;
                    llSafeguardTitle.setLayoutParams(ll);
                }
                title = (TextView) itemView.findViewById(R.id.tv_title);
                detail = (TextView) itemView.findViewById(R.id.tv_detail);
                title.setText(data.get(i).getName());
                detail.setText(data.get(i).getDetail());
                linearLayout.addView(itemView);
            }
        }
    }

    public static DialogSafeguard showAsDropDown(FragmentActivity activity, List<Guarantee> guaranteeList, View targetView) {
        int[] targetLocation = new int[2];
        targetView.getLocationOnScreen(targetLocation);
        int offsetY = 0;
        try {
            offsetY = targetLocation[1];
        } catch (Exception e) {

        }

        DialogSafeguard dialogSafeguard = DialogSafeguard.newInstance(guaranteeList, offsetY + targetView.getBottom());
        dialogSafeguard.show(activity.getSupportFragmentManager(), "");
        return dialogSafeguard;

    }
}
